// page/me//pages/sm_renzheng/sm_renzheng.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    images:[],
    images_two:[],
  },
  //点击事件
  chooseImage(e) {
    console.log(e);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        const images = res.tempFilePaths;
        console.log(res);
        this.data.images = images.length <= 3 ? images : images.slice(0, 3)
        this.setData({
          images: this.data.images
        })
        // this.submitForm();
      }
    })
  },
  submitForm(e) {
    const arr = []
    console.log(1);
    var uat = wx.getStorageSync('uat');
    for (let path of this.data.images) {
      console.log(path);
      var that = this;
      arr.push(wx.uploadFile({
        header: { 'chartset': 'utf-8' },
        url: app.globalData.publicjs.datacenter_api_url + 'api/Sellerjoininc2c/upload_image',
        filePath: path,
        name: 'image',
        formData: {
          'access_token': uat
        },
        success: function (res) {
          console.log(res.data);
          var jsonStr = res.data;
          jsonStr = jsonStr.replace(" ", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
            var jj = JSON.parse(jsonStr);
            res.data = jj;
            that.setData({
              store_avatar: res.data.data
            })
          }
          wx.showToast({
            title: '上传成功',
            icon: 'none'
          });
          console.log(res.data);
        },
        fail: function (res) {
          console.log(res);
          console.log('接口调用失败');
        }
      }))
    }
  },
  chooseImage_two(e) {
    console.log(e);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        const images_two = res.tempFilePaths;
        console.log(res);
        this.data.images_two = images_two.length <= 3 ? images_two : images_two.slice(0, 3)
        this.setData({
          images_two: this.data.images_two
        })
        // this.submitForm();
      }
    })
  },
  submitForm_two(e) {
    const arr = []
    console.log(1);
    var uat = wx.getStorageSync('uat');
    for (let path of this.data.images) {
      console.log(path);
      var that = this;
      arr.push(wx.uploadFile({
        header: { 'chartset': 'utf-8' },
        url: app.globalData.publicjs.datacenter_api_url + 'api/Sellerjoininc2c/upload_image',
        filePath: path,
        name: 'image',
        formData: {
          'access_token': uat
        },
        success: function (res) {
          console.log(res.data);
          var jsonStr = res.data;
          jsonStr = jsonStr.replace(" ", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, "");//重点
            var jj = JSON.parse(jsonStr);
            res.data = jj;
            that.setData({
              store_avatar: res.data.data
            })
          }
          wx.showToast({
            title: '上传成功',
            icon: 'none'
          });
          console.log(res.data);
        },
        fail: function (res) {
          console.log(res);
          console.log('接口调用失败');
        }
      }))
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})